<template>
  <div>
    <el-dialog :title="info.isAdd ? '添加' : '编辑'" :visible.sync="info.isShow" @close="close">
      <div>{{ user }} {{ attrsArr }}</div>
      <el-form :model="user" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="user.specsname"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          v-for="(item, index) in attrsArr"
          :key="index"
        >
          <div class="goodsAttrBox">
            <el-input v-model="item.val"></el-input>
            <el-button
              type="primary"
              @click="attrsArr.push({ val: `` })"
              v-if="index == 0"
              >新增规格属性</el-button
            >
            <el-button type="danger" @click="attrsArr.splice(index, 1)" v-else
              >删除</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" type="primary" @click="add"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import path from "path";
import { mapGetters, mapActions } from "vuex";
import { reqspecsadd, reqspecsinfo, reqspecsedit } from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins/index";
export default {
  mixins: [formClose],
  props: ["info"],
  data() {
    return {
      user: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      attrsArr: [{ val: "" }, { val: "" }],
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {},
  methods: {
    ...mapActions({ getList: "specs/getList" }),
    //清空表单
    empty() {
      this.user = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      this.attrsArr = [{ val: "" }, { val: "" }];
    },
    //获取一条用户信息
    async getOne(id) {
      let { data: res } = await reqspecsinfo({ id });
      if (res.code == 200) {
        this.user = res.list[0];
        this.attrsArr= this.user.attrs.map(item => ({val:item}));
      }
    },
    //添加
    async add() {
      //处理attrsArr数组
      this.user.attrs = this.attrsArr.map((item) => item.val).join(",");
      let { data: res } = await reqspecsadd(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.getList();
      }
    },
    //修改
    async update() {
      //处理attrsArr数组
      this.user.attrs = this.attrsArr.map((item) => item.val).join(",");
      let { data: res } = await reqspecsedit(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.getList();
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.goodsAttrBox {
  display: flex;
}
</style>